package com.love.marriage.userInfo.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSort;
import com.love.marriage.common.interfaces.avoid.AvoidRepeatableCommit;
import com.love.marriage.common.interfaces.avoid.LogRestRequest;
import com.love.marriage.common.validator.Insert;
import com.love.marriage.common.validator.Update;
import com.love.marriage.common.vo.Result;
import com.love.marriage.common.vo.ResultBoolean;
import com.love.marriage.oss.vo.FileInfoVo;
import com.love.marriage.personalImageryInfo.vo.ResultPersonalImageryInfoVo;
import com.love.marriage.userInfo.service.UserInfoService;
import com.love.marriage.userInfo.vo.*;
import com.love.marriage.userRechargeRecordInfo.vo.UserRechargeHierarchyQueryParam;
import com.love.marriage.userRechargeRecordInfo.vo.UserRechargeHierarchyVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpHeaders;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * description:  用户表restApi
 * author:       mybatisPlus
 * createDate:   2024-10-11 22:04:00
 * version:      1.0.0
 */
@ApiSort(105)
@Tag(name = "用户表相关接口", description = "用户表相关接口")
@RestController
@RequiredArgsConstructor
@RequestMapping("/user-info")
public class UserInfoController {

    private final UserInfoService userInfoService;

    @LogRestRequest(apiName = "获取用户表分页")
    @ApiOperationSupport(order = 10, author = "alex")
    @Operation(summary = "获取用户表分页", description = "获取用户表分页")
    @ApiResponse(responseCode = "200", description = "Success",
            content = @Content(mediaType = "application/json",
                    schema = @Schema(implementation = ResultUserInfoVoPage.class
                    )))
    @PostMapping(value = "/page")
    @Parameters({
            @Parameter(name = "pageNum", description = "页码", required = false),
            @Parameter(name = "pageSize", description = "每页大小", required = false)
    })
    public Result<Page<UserInfoVo>> getPage(@RequestParam(value = "pageNum", required = false) Long pageNum,
                                            @RequestParam(value = "pageSize", required = false) Long pageSize,
                                            @RequestBody(required = false) UserInfoVo userInfoVo) {
        return Result.success(userInfoService.getPage(pageNum, pageSize, userInfoVo));
    }

    @LogRestRequest(apiName = "获取用户列表")
    @ApiOperationSupport(order = 10, author = "alex")
    @Operation(summary = "获取用户列表", description = "获取用户列表",
            requestBody = @io.swagger.v3.oas.annotations.parameters.RequestBody(
                    description = "查询条件",
                    required = true,
                    content = @Content(schema = @Schema(implementation = UserSearchInfoVo.class, requiredProperties = {"userId"}))
            ))
    @ApiResponse(responseCode = "200", description = "Success",
            content = @Content(mediaType = "application/json",
                    schema = @Schema(implementation = ResultUserInfoVoPage.class
                    )))
    @PostMapping(value = "/getUserPage")
    @Parameters({
            @Parameter(name = "pageNum", description = "页码", required = false),
            @Parameter(name = "pageSize", description = "每页大小", required = false)
    })
    @Tag(name = "用户", description = "用户信息")
    public Result<Page<UserVo>> getUserPage(@RequestParam(value = "pageNum", required = false) Long pageNum,
                                            @RequestParam(value = "pageSize", required = false) Long pageSize,
                                            @RequestBody(required = false) UserSearchInfoVo userInfoVo) {
        return Result.success(userInfoService.getUserPage(pageNum, pageSize, userInfoVo));
    }

    @LogRestRequest(apiName = "获取用户表详情")
    @ApiOperationSupport(order = 20, author = "alex")
    @Operation(summary = "获取用户表详情", description = "获取用户表详情")
    @ApiResponse(responseCode = "200", description = "Success",
            content = @Content(mediaType = "application/json",
                    schema = @Schema(implementation = ResultUserInfoVo.class
                    )))
    @Parameters({
            @Parameter(name = "id", description = "id", required = true, schema = @Schema(implementation = Long.class, type = "Long", example = "1"))
    })
    @GetMapping
    public Result<UserInfoVo> query(@RequestParam(value = "id") Long id) throws Exception {
        return Result.success(userInfoService.queryUserInfo(id));
    }

    @LogRestRequest(apiName = "查询用户详情")
    @ApiOperationSupport(order = 20, author = "alex")
    @Operation(summary = "查询用户详情", description = "查询用户详情")
    @ApiResponse(responseCode = "200", description = "Success",
            content = @Content(mediaType = "application/json",
                    schema = @Schema(implementation = ResultUserVo.class
                    )))
    @Parameters({
            @Parameter(name = "id", description = "id", required = true, schema = @Schema(implementation = Long.class, type = "Long", example = "1"))
    })
    @Tag(name = "用户", description = "用户信息")
    @Tag(name = "我的", description = "我的")
    @GetMapping(value = "/queryUser")
    public Result<UserVo> queryUser(@RequestParam(value = "id") Long id) throws Exception {
        return Result.success(userInfoService.queryNewUser(id));
    }

    @LogRestRequest(apiName = "查询用户列表")
    @ApiOperationSupport(order = 25, author = "alex")
    @Operation(summary = "查询用户列表", description = "查询用户列表",
            requestBody = @io.swagger.v3.oas.annotations.parameters.RequestBody(
                    description = "查询条件",
                    required = true,
                    content = @Content(schema = @Schema(implementation = UserInfoVo.class))
            ))
    @ApiResponse(responseCode = "200", description = "Success",
            content = @Content(mediaType = "application/json",
                    schema = @Schema(implementation = ResultUserVo.class
                    )))
    @Tag(name = "用户", description = "用户信息")
    @Tag(name = "我的", description = "我的")
    @PostMapping(value = "/queryUserList")
    public Result<List<UserInfoVo>> queryUserList(@RequestBody UserInfoVo userInfoVo) throws Exception {
        return Result.success(userInfoService.queryUserList(userInfoVo));
    }

    @LogRestRequest(apiName = "新增用户表")
    @SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
    @AvoidRepeatableCommit
    @ApiOperationSupport(order = 30, author = "alex")
    @Operation(summary = "新增用户表", description = "新增用户表",
            requestBody = @io.swagger.v3.oas.annotations.parameters.RequestBody(
                    description = "查询条件",
                    required = false,
                    content = @Content(schema = @Schema(implementation = UserInfoVo.class))
            ))
    @ApiResponse(responseCode = "200", description = "Success",
            content = @Content(schema = @Schema(implementation = ResultUserInfoVo.class)))
    @PostMapping
    public Result<UserInfoVo> add(@Validated({Insert.class}) @RequestBody UserInfoVo userInfoVo) {
        return Result.success(userInfoService.addUserInfo(userInfoVo));
    }

    @LogRestRequest(apiName = "修改用户表")
    @ApiOperationSupport(order = 40, author = "alex")
    @Operation(summary = "修改用户表", description = "修改用户表",
            requestBody = @io.swagger.v3.oas.annotations.parameters.RequestBody(
                    description = "查询条件",
                    required = false,
                    content = @Content(schema = @Schema(implementation = UserInfoVo.class))
            ))
    @ApiResponse(responseCode = "200", description = "Success",
            content = @Content(schema = @Schema(implementation = ResultUserInfoVo.class)))
    @PutMapping
    public Result<UserInfoVo> update(@Validated({Update.class}) @RequestBody UserInfoVo userInfoVo) {
        return Result.success(userInfoService.updateUserInfo(userInfoVo));
    }

    @LogRestRequest(apiName = "刪除用户表")
    @ApiOperationSupport(order = 50, author = "alex")
    @Operation(summary = "刪除用户表", description = "刪除用户表")
    @Parameters({
            @Parameter(name = "ids", description = "ids,用，分割", required = true,
                    schema = @Schema(implementation = String.class, type = "String", example = "1,2"))
    })
    @ApiResponse(responseCode = "200", description = "Success",
            content = @Content(schema = @Schema(implementation = ResultBoolean.class)))
    @DeleteMapping
    public Result<Boolean> delete(@RequestParam("ids") String ids) {
        return Result.success(userInfoService.deleteUserInfo(ids));
    }

    @LogRestRequest(apiName = "添加个人头像")
    @AvoidRepeatableCommit
    @ApiOperationSupport(order = 60, author = "mybatisPlus")
    @Operation(summary = "添加个人头像", description = "添加个人头像")
    @ApiResponse(responseCode = "200", description = "Success",
            content = @Content(schema = @Schema(implementation = ResultPersonalImageryInfoVo.class)))
    @PostMapping(value = "/addAvatar")
    @Parameters({
            @Parameter(name = "userId", description = "用户id", required = false)
    })
    @Tag(name = "用户", description = "用户信息")
    public Result<FileInfoVo> addAvatar(@RequestParam(value = "userId") Long userId,
                                        @RequestPart(value = "file") MultipartFile file) throws Exception {
        return Result.success(userInfoService.addAvatar(userId, file));
    }

    @LogRestRequest(apiName = "获取用户统计")
    @ApiOperationSupport(order = 70, author = "mybatisPlus")
    @Operation(summary = "获取用户统计", description = "获取用户统计")
    @ApiResponse(responseCode = "200", description = "Success",
            content = @Content(schema = @Schema(implementation = UserCountVo.class)))
    @GetMapping(value = "/queryUserMemberInfo")
    @Tag(name = "用户", description = "用户信息")
    public Result<UserCountVo> queryUserMemberInfo() {
        return Result.success(userInfoService.queryUserMemberInfo());
    }

    @LogRestRequest(apiName = "获取用户总览")
    @ApiOperationSupport(order = 80, author = "mybatisPlus")
    @Operation(summary = "获取用户总览", description = "获取用户总览")
    @ApiResponse(responseCode = "200", description = "Success",
            content = @Content(schema = @Schema(implementation = UserCountVo.class)))
    @GetMapping(value = "/queryUserStatistics")
    @Tag(name = "用户", description = "用户信息")
    public Result<UserCountVo> queryUserStatistics() {
        return Result.success(userInfoService.queryUserStatistics());
    }

    @LogRestRequest(apiName = "获取日用户变化")
    @ApiOperationSupport(order = 90, author = "mybatisPlus")
    @Operation(summary = "获取日用户变化", description = "获取日用户变化")
    @ApiResponse(responseCode = "200", description = "Success",
            content = @Content(schema = @Schema(implementation = Result.class)))
    @GetMapping(value = "/queryDayUserInfo")
    @Tag(name = "用户", description = "用户信息")
    public Result<List<UserDataUserVo>> queryDayUserInfo() {
        return Result.success(userInfoService.queryDayUserInfo());
    }

    @LogRestRequest(apiName = "获取邀请列表")
    @ApiOperationSupport(order = 100, author = "alex")
    @Operation(summary = "获取邀请列表", description = "获取邀请列表")
    @ApiResponse(responseCode = "200", description = "Success",
            content = @Content(mediaType = "application/json",
                    schema = @Schema(implementation = ResultUserInfoVoPage.class
                    )))
    @PostMapping(value = "/inviteList")
    @Tag(name = "回佣", description = "回佣相关接口")
    public Result<List<UserInfoVo>> getInvite(@RequestBody(required = false) UserInfoVo userInfoVo) {
        return Result.success(userInfoService.getInvite(userInfoVo));
    }

    @LogRestRequest(apiName = "查询用户的邀请用户的所有消费信息")
    @ApiOperationSupport(order = 110, author = "alex")
    @Operation(summary = "查询用户的邀请用户的所有消费信息", description = "查询用户的邀请用户的所有消费信息")
    @Parameters({
            @Parameter(name = "pageNum", description = "页码", required = false),
            @Parameter(name = "pageSize", description = "每页大小", required = false)
    })
    @ApiResponse(responseCode = "200", description = "Success",
            content = @Content(schema = @Schema(implementation = Result.class)))
    @PostMapping(value = "/queryUserReduceInfo")
    @Tag(name = "用户", description = "用户信息")
    public Result<Page<UserReduceInfoVo>> queryUserReduceInfo(@RequestParam(value = "pageNum", required = false) Long pageNum,
                                                              @RequestParam(value = "pageSize", required = false) Long pageSize,
                                                              @RequestBody UserInfoVo userInfoVo) {
        return Result.success(userInfoService.queryUserReduceInfo(pageNum, pageSize, userInfoVo));
    }

    @LogRestRequest(apiName = "查询用户的邀请用户的消费明细信息")
    @ApiOperationSupport(order = 115, author = "alex")
    @Operation(summary = "查询用户的邀请用户的消费明细信息", description = "查询用户的邀请用户的消费明细信息")
    @Parameters({
            @Parameter(name = "pageNum", description = "页码", required = false),
            @Parameter(name = "pageSize", description = "每页大小", required = false)
    })
    @ApiResponse(responseCode = "200", description = "Success",
            content = @Content(schema = @Schema(implementation = Result.class)))
    @PostMapping(value = "/queryUserReduceDetailInfo")
    @Tag(name = "用户", description = "用户信息")
    public Result<Page<UserReduceInfoVo>> queryUserReduceDetailInfo(@RequestParam(value = "pageNum", required = false) Long pageNum,
                                                                    @RequestParam(value = "pageSize", required = false) Long pageSize,
                                                                    @RequestBody UserInfoVo userInfoVo) {
        return Result.success(userInfoService.queryUserReduceDetailInfo(pageNum, pageSize, userInfoVo));
    }

    /**
     * 充值层级统计列表
     */
    @LogRestRequest(apiName = "充值层级统计分页")
    @ApiOperationSupport(order = 60, author = "mybatisPlus")
    @Operation(summary = "充值层级统计分页", description = "充值层级统计分页")
    @ApiResponse(responseCode = "200", description = "Success",
            content = @Content(mediaType = "application/json",
                    schema = @Schema(implementation = UserRechargeHierarchyVo.class)))
    @PostMapping("/recharge-hierarchy/page")
    public Result<Page<UserRechargeHierarchyVo>> getRechargeHierarchyPage(@RequestParam(value = "pageNum", required = false) Long pageNum,
                                                                          @RequestParam(value = "pageSize", required = false) Long pageSize,
                                                                          @RequestBody UserRechargeHierarchyQueryParam param) {
        return Result.success(userInfoService.queryUserRechargeHierarchyPage(pageNum, pageSize, param));
    }

    /**
     * 充值层级统计列表
     */
    @LogRestRequest(apiName = "充值层级统计列表")
    @ApiOperationSupport(order = 60, author = "mybatisPlus")
    @Operation(summary = "充值层级统计列表", description = "查询充值层级统计列表")
    @ApiResponse(responseCode = "200", description = "Success",
            content = @Content(mediaType = "application/json",
                    schema = @Schema(implementation = UserRechargeHierarchyVo.class)))
    @PostMapping("/recharge-hierarchy/list")
    public Result<List<UserRechargeHierarchyVo>> getRechargeHierarchyList(@RequestBody UserRechargeHierarchyQueryParam param) {
        return Result.success(userInfoService.queryUserRechargeHierarchyList(param));
    }

    /**
     * 充值层级统计Excel导出
     */
    @LogRestRequest(apiName = "充值层级统计Excel导出")
    @ApiOperationSupport(order = 61, author = "mybatisPlus")
    @Operation(summary = "充值层级统计Excel导出", description = "导出充值层级统计Excel")
    @PostMapping("/recharge-hierarchy/export")
    public void exportRechargeHierarchyExcel(@RequestBody UserRechargeHierarchyQueryParam param, HttpServletResponse response) {
        userInfoService.exportUserRechargeHierarchyExcel(param, response);
    }

    /**
     * 充值层级统计Excel导出
     */
    @LogRestRequest(apiName = "邀请明细")
    @ApiOperationSupport(order = 70, author = "mybatisPlus")
    @Operation(summary = "邀请明细", description = "邀请明细")
    @PostMapping("/invite_detail")
    public InviteCommissionVo inviteCommissionDetail(@RequestBody UserRechargeHierarchyQueryParam param) {
        return userInfoService.inviteCommissionDetail(param);
    }

    // 导出用户信息
    @LogRestRequest(apiName = "导出用户信息")
    @ApiOperationSupport(order = 80, author = "mybatisPlus")
    @Operation(summary = "导出用户信息", description = "导出用户信息")
    @PostMapping("/export-user-info")
    public void exportUserInfo(@RequestBody UserInfoVo userInfoVo, HttpServletResponse response) {
        userInfoService.exportUserInfo(userInfoVo, response);
    }

    @LogRestRequest(apiName = "获取用户所有信息")
    @ApiOperationSupport(order = 90, author = "mybatisPlus")
    @Operation(summary = "获取用户所有信息", description = "获取用户所有信息")
    @ApiResponse(responseCode = "200", description = "Success",
            content = @Content(mediaType = "application/json",
                    schema = @Schema(implementation = ResultUserInfoVoPage.class
                    )))
    @PostMapping("/get-all-user-info")
    @Parameters({
            @Parameter(name = "pageNum", description = "页码", required = false),
            @Parameter(name = "pageSize", description = "每页大小", required = false)
    })
    public Result<Page<UserInfoVo>> getUserAllInfo(@RequestParam(value = "pageNum", required = false) Long pageNum,
                                                   @RequestParam(value = "pageSize", required = false) Long pageSize,
                                                   @RequestBody UserAllQueryVo userAllQueryVo) {
        return Result.success(userInfoService.getUserAllInfo(pageNum, pageSize, userAllQueryVo));
    }

    @LogRestRequest(apiName = "导出用户完整信息")
    @ApiOperationSupport(order = 91, author = "mybatisPlus")
    @Operation(summary = "导出用户完整信息", description = "导出用户完整信息（包含会员、红娘、合伙人、实名认证、用户资料信息）")
    @PostMapping("/export-all-user-info")
    public void exportUserAllInfo(@RequestBody UserAllQueryVo userAllQueryVo, HttpServletResponse response) {
        userInfoService.exportUserAllInfo(userAllQueryVo, response);
    }
}
